package data;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * This is the job class. This class keep track of the job description, the volunteer, leader status,
 * time of job, whether the job has been verify, the job's LMA, the set size of the volunteer,
 * and the current volunteer size.
 * 
 * @author Minh Thai
 *
 */
public class Job implements Comparable<Job> {
	/**
	 * The Jobs name.
	 */
	private String name;
	/**
	 * The LMA's phone number that posted the Job.
	 */
	private String lmaPhone;
	/**
	 * Location of the job.
	 */
	private String location;
	/**
	 * Grade of the job.
	 */
	private int grade;
	/**
	 * Difficulty of the job.
	 */
	private int difficulty;
	/**
	 * Description of the job.
	 */
	private String description;
	/**
	 * List of volunteers signed for the job.
	 */
	private Volunteer[] volunteerList;
	/**
	 * Boolean field to specify if a job a leader assigned.
	 */
	private boolean hasLeader;
	/**
	 * Volunteer that may be a leader.
	 */
	private Volunteer leader;
	/**
	 * Date of the job.
	 */
	private String date;
	/**
	 * Time of the job.
	 */
	private String time;
	/**
	 * Boolean field to check is the job has be scheduled or not.
	 */
	private boolean scheduled;
	/**
	 * LMA's name.
	 */
	private String LMA;
	/**
	 * Size of the job.
	 */
	private int jobSize; 
	/**
	 * Current number of volunteer signed for a job.
	 */
	private int currentVolunteerSize; 
	
	/**
	 * The alternative constructor for Job, this accept parameters to initialize the
	 * job description, the date, the LMA, and the job size (maximum of volunteers allow).
	 * It is Scheduler's job to check for valid date before approving
	 * 
	 * @param description the description of the job
	 * @param time the date of the job
	 * @param LMA the requester of the job
	 * @param jobSize the maximum number of volunteers allow for the job
	 * 
	 */
	public Job(String name, String lmaPhone, String location, int grade, int difficulty, 
				String description, String date, String time, String LMA, int jobSize) {
		if (description != null && description!= null && !description.trim().equals("") &&
				time != null && LMA != null && !LMA.trim().equals("")
				&& jobSize > 0
				&& name != null && location != null && grade >= 0 && difficulty >= 0
				&& !name.trim().equals("") && !location.trim().equals("")) {
			this.description = description;
			this.date = date;
			this.time = time;
			this.LMA = LMA;
			this.jobSize = jobSize;
			this.name = name;
			this.location = location;
			this.grade = grade;
			this.difficulty = difficulty;
			this.volunteerList = new Volunteer[jobSize];
			this.hasLeader = false;
			this.scheduled = false;
			this.leader = null;
			this.currentVolunteerSize = 0;
		}else {
			this.time = null;
			this.description = null;
			this.name = "";
			this.location = "";
			this.grade = 0;
			this.difficulty = 0;
			this.hasLeader = false;
			this.LMA = null;
			this.volunteerList = null;
			this.leader = null;
			this.jobSize = 1;
			this.scheduled = false;
			this.currentVolunteerSize = 0;
		}
		this.lmaPhone = lmaPhone;
	}
	
	/**
	 * The alternative constructor for Job, this accept parameters to initialize the
	 * job description, the date, the LMA, and the job size (maximum of volunteers allow).
	 * It is Scheduler's job to check for valid date before approving
	 * This constructor only to be used when loading.
	 * 
	 * @param description the description of the job
	 * @param time the date of the job
	 * @param LMA the requester of the job
	 * @param jobSize the maximum number of volunteers allow for the job
	 * 
	 */
	public Job(String name, String lmaPhone, String location, int grade, int difficulty, 
				String description, String date, String time, String LMA, int jobSize, 
				boolean verified, boolean hasLeader, ArrayList<Volunteer> volunteers) {
		this.description = description;
		this.date = date;
		this.time = time;
		this.LMA = LMA;
		this.jobSize = jobSize;
		this.name = name;
		this.lmaPhone = lmaPhone;
		this.location = location;
		this.grade = grade;
		this.difficulty = difficulty;
		this.volunteerList = new Volunteer[jobSize];
		this.hasLeader = false;
		this.scheduled = false;
		this.scheduled = verified;
		this.hasLeader = hasLeader;
		this.currentVolunteerSize = 0;
		int i = 1;

		if(volunteers.size() > 0) {
			if(hasLeader) {
				i = 0;
			}
			for(int j = 0; j < volunteers.size(); j++) {
				volunteerList[i] = volunteers.get(j);
				currentVolunteerSize++;
				i++;
				
			}
		}
		this.leader = volunteerList[0];
	
	}
	
	/**
	 * This method allow the user to add a volunteer to the list if the maximum number of 
	 * volunteer set for this job has not been reach
	 * 
	 * @param person the Volunteer to add to the job 
	 * 
	 */
	public boolean addVolunteer(Volunteer person) {
		if (person != null && person.getUsername() != null &&
				!person.getUsername().trim().equals("")) {
			if (this.volunteerList != null) {
				for(Volunteer x : volunteerList) {
					if (x != null && x.equals(person)) {
						return false;
					}
				}
			}
			if (volunteerList == null) {
				volunteerList = new Volunteer[jobSize];
				volunteerList[currentVolunteerSize + 1] = person;
				currentVolunteerSize++;
				return true;
			}else if (currentVolunteerSize < jobSize - 1) { 
				volunteerList[currentVolunteerSize + 1] = person; 
				currentVolunteerSize++;					
				return true;
			}
		}
		return false;
	}
	
	/**
	 * This method allow the user to remove a volunteer from the current job
	 * 
	 * @param person the Volunteer to remove
	 * 
	 */
	public void removeVolunteer(Volunteer person) {
		if (person != null && person.getUsername() != null &&
				!person.getUsername().trim().equals("") && 
				volunteerList != null) {
				Volunteer[] temp = new Volunteer[volunteerList.length];
				int count = 1;
				for(Volunteer x : volunteerList) {
					if (x != null && !x.equals(person)) {
						temp[count] = x;
						count++;
					}else if (x != null && x.equals(person)){
						currentVolunteerSize--;
					}
				}
				volunteerList = temp;
			}	
	}
	
	/**
	 * This return the string that describe the job, this is use to save the Job
	 * 
	 */
	public String toString() {
		
		String output = "Job,," + name + ",," + lmaPhone + ",," + location + ",," + grade + ",," +
		difficulty + ",," + description + ",," + date + ",," +  time +
		",," + LMA + ",," + jobSize + ",," + scheduled + ",," 
		+ hasLeader;
		int i = 0;
		if(!hasLeader) {
			i = 1;
		}
		for(; i < volunteerList.length; i++) {
			if(volunteerList[i] != null) {
				output += ",," + volunteerList[i].getUsername();
			}
		}
		return output + "\n";
	}
	
	
	/**
	 * This method help compare if another job object is equivalent to this job
	 * 
	 * @return if the object is equal
	 * 
	 */
	public boolean equals(Object x) {
		if (x != null && x instanceof Job) {
			Job j = (Job) x;
			if (j.isScheduled() != this.scheduled ||
				!j.getDescription().equals(this.description) ||
				!j.getName().equals(this.name) ||
				!j.getLocation().equals(this.location) ||
				j.getGrade() != this.grade ||
				j.getDifficulty() != this.difficulty ||
				j.getHasLeader() != this.hasLeader ||
				!j.getTime().equals(this.time) ||
				!j.getDate().equals(this.date) ||
				!j.getLMA().equals(this.LMA) ||
				j.getJobSize() != this.jobSize ||
				j.getCurrentVolunteerSize() != this.currentVolunteerSize) {
				
				return false;
			}
			if (j.getLeader() != null && this.leader != null &&
				!j.getLeader().getUsername().equals(this.getLeader().getUsername())) {
				return false;
			}
			if (!Arrays.equals(j.getVolunteerList(), this.volunteerList)) {
				return false;
			}
		}
	return true;
	}
	
	/**
	 * This method return the description of the job, which includes the the description of
	 * the job, the number of volunteer needed, the time, job verification status, the LMA
	 * that request this job, and the current size of volunteer
	 * 
	 * @return the String that contains all information regarding the job
	 * 
	 */
	public String getInfo() {
		
		String info = "";
		
		if (this.location != null && grade >= 0 && difficulty >= 0 && name !=null) {
			if (this.LMA != null) {
				info += String.format("%-50s%-35s\n", "Land Management Agency:", this.LMA);
			}
			info += String.format("%-50s%-35s\n", "Verify Status:", this.scheduled);
			info += String.format("%-50s%-35s\n", "Job Name:", this.name);
			info += String.format("%-50s%-35s\n", "LMA Phone:", this.lmaPhone);
			info += String.format("%-50s%-35s\n", "Location:", this.location);
			info += String.format("%-50s%-35s\n", "Grade:", this.grade);
			info += String.format("%-50s%-35s\n", "Difficulty:", this.difficulty);
		}		
		if (this.time != null) {
			info += String.format("%-50s%-35s\n", "Date:", getDate());
			info += String.format("%-50s%-35s\n", "Time:", this.time);
		}
		info += String.format("%-50s%-35s\n", "Has Leader:", this.hasLeader);
		if (this.leader != null) {
			info += String.format("%-50s%-35s\n", "Leader:", this.leader.getFirstname());
		}
		info += String.format("%-50s%-35s\n", "Number of Volunteer need:", this.jobSize);
		info += String.format("%-50s%-35s\n", "Current Volunteer Size:", this.currentVolunteerSize);
		info += String.format("%-50s%-35s\n", "Job Description:", getDescription());
		return info;
		
	}
	
	/**
	 * This return a boolean that indicate whether the job has been scheduled
	 * 
	 * @return true or false
	 * 
	 */
	public boolean isScheduled() {
		return scheduled;
	}

	/**
	 * This let the user set status of whether the job has been schedule
	 * 
	 * @param scheduled true or false
	 * 
	 */
	public void setScheduled(boolean scheduled) {
		this.scheduled = scheduled;
	}

	/**
	 * This let the user set the description of the job
	 * 
	 * @param description the description
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * This let the user initialize the list of volunteer for the job
	 * 
	 * @param volunteerList an arrayList of volunteer
	 * 
	 */
	public void setVolunteerList(Volunteer[] volunteerList) {
		this.volunteerList = volunteerList;
	}

	/**
	 * This allow the user to set the date for the job
	 * 
	 * @param time is the Date string
	 * 
	 */
	public void setDateTime(String date, String time) {
		this.date = date;
		this.time = time;
	}

	/**
	 * This allow the user to set the current volunteer size limit for the job
	 * 
	 * @param currentVolunteerSize the volunteer number limit
	 * 
	 */
	public void setCurrentVolunteerSize(int currentVolunteerSize) {
		this.currentVolunteerSize = currentVolunteerSize;
	}
	
	/**
	 * This method allow the user to modify the description of the job
	 * 
	 * @param jobDescription the new job description
	 * 
	 */
	public void modifyDescription(String jobDescription) {
		if (jobDescription != null && !jobDescription.trim().equals("")) {
			this.description = jobDescription;
		}
	}
	
	/**
	 * This method return the current description for the following job, no extra informations is provided
	 * 
	 * @return only the description of job
	 * 
	 */
	public String getDescription() {
		return this.description;
	}

	/**
	 * This method allow the user to change the date of volunteer 
	 * 
	 */
	public void addDateTIme(String date, String time) {
		this.setDateTime(date, time);
	}

	/**
	 * This method return the list of volunteers
	 * 
	 * @return the array of Volunteer objects
	 * 
	 */
	public Volunteer[] getVolunteerList() {
		return this.volunteerList;
	}

	/**
	 * This method return a boolean to indicate if the job has been verify
	 * 
	 * @return the status of job verify
	 * 
	 */
	public boolean getVerified() {
		return this.scheduled;
	}
	
	/**
	 * This method return a boolean that indicate whether the job has leader 
	 * 
	 * @return status of job's leader
	 * 
	 */
	public boolean getHasLeader() {
		return this.hasLeader;
	}

	/**
	 * This method set the leader status of the job
	 * 
	 * @param hasLeader the status of leader
	 * 
	 */
	public void setHasLeader(boolean hasLeader) {
		this.hasLeader = hasLeader;
	}

	/**
	 * This method return the scheduled time
	 * 
	 * @return String indicating the time
	 * 
	 */
	public String getTime() {
			return this.time;
	}
	
	/**
	 * This method return the date of the job
	 * 
	 * @return String indicating the date
	 * 
	 */
	public String getDate() {
		return date;
	}

	/**
	 * This method return a boolean to indicate whether the job has been verified
	 * 
	 * @return job verification status
	 * 
	 */
	public boolean isVerified() {
		return scheduled;
	}

	/**
	 * This method let user set the job verification status
	 * 
	 * @param verified the job verification status
	 * 
	 */
	public void setVerified(boolean verified) {
		this.scheduled = verified;
	}

	/**
	 * This method return the String of job's LMA 
	 * 
	 * @return the job's LMA
	 * 
	 */
	public String getLMA() {
		return LMA;
	}

	/**
	 * This method allow the user to set the LMA for the job
	 * 
	 * @param lMA the name or username of LMA of this job
	 * 
	 */
	public void setLMA(String lMA) {
		LMA = lMA;
	}

	/**
	 * This method return the volunteer cap size for this job
	 * 
	 * @return the volunteer limit
	 * 
	 */
	public int getJobSize() {
		return jobSize;
	}

	/**
	 * This method let user set the volunteer cap size for this job
	 * 
	 * @param jobSize the volunteer limit
	 * 
	 */
	public void setJobSize(int jobSize) {
		this.jobSize = jobSize;
	}

	/**
	 * This method return the current number of volunteers currently enlisted for this job
	 * 
	 * @return the number of volunteer
	 * 
	 */
	public int getCurrentVolunteerSize() {
		return currentVolunteerSize;
	}
	
	/**
	 * This return the name of the job
	 * 
	 * @return String name of job
	 * 
	 */
	public String getName() {
		return name;
	}

	/**
	 * This allow for the name of the job to be set
	 * 
	 * @param name the name of this job
	 * 
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * This return the location that the job take place
	 * 
	 * @return the location
	 * 
	 */
	public String getLocation() {
		return location;
	}
	
	/**
	 * This return the leader leader Volunteer object, null if has no leader
	 * 
	 * @return the Volunteer object of leader
	 * 
	 */
	public Volunteer getLeader() {
		return leader;
	}

	/**
	 * Set the leader boolean field.
	 * 
	 * @param leader
	 * 
	 */
	public void setLeader(Volunteer leader) {
		if (this.leader != null) {
			this.leader = leader;
		}
	}

	/**
	 * This allow the location of the job to be set
	 * 
	 * @param location the location of job
	 * 
	 * 
	 */
	public void setLocation(String location) {
		this.location = location;
	}

	/**
	 * This return the grade of the job
	 * 
	 * @return grade
	 */
	public int getGrade() {
		return grade;
	}

	/**
	 * This allow for the grade to be set for this job
	 * 
	 * @param grade the grade
	 * 
	 */
	public void setGrade(int grade) {
		this.grade = grade;
	}

	/**
	 * This return the difficulty of the job
	 * 
	 * @return the job's difficulty
	 * 
	 */
	public int getDifficulty() {
		return difficulty;
	}

	/**
	 * This allow the difficulty of the job to be set
	 * 
	 * @param difficulty the difficulty to be set to
	 * 
	 */
	public void setDifficulty(int difficulty) {
		this.difficulty = difficulty;
	}
	
	/**
	 * Override compareTo method to allow for Job to be sort 
	 * 
	 */
	public int compareTo(Job o) {
		// TODO Auto-generated method stub
		return this.time.compareTo(o.time);
	}
}
